package cn.wizzer.app.web.modules.controllers.platform.user.mall;

import cn.wizzer.app.user.modules.models.enums.ExchangeEnum;
import cn.wizzer.app.user.modules.services.account.ExchangeService;
import cn.wizzer.app.web.commons.ulog.annotation.SLog;
import cn.wizzer.framework.base.Result;
import com.alibaba.dubbo.config.annotation.Reference;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.nutz.dao.Chain;
import org.nutz.dao.Cnd;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.lang.Times;
import org.nutz.log.Log;
import org.nutz.log.Logs;
import org.nutz.mvc.annotation.*;

@IocBean
@At("/platform/user/mall/exchange")
public class ExchangeController {
    private static final Log log = Logs.get();

    @Inject
    @Reference
    private ExchangeService exchangeService;

    @At("/")
    @Ok("beetl:/platform/user/mall/exchange/index.html")
    @RequiresPermissions("user.mall.exchange.list")
    public void index() {
    }


    /**
     * 兑换记录查询
     * @param status String 兑换状态
     * @param pageNumber int
     * @param pageSize int
     * @return Object
     */
    @At
    @Ok("json:full")
    @RequiresPermissions("user.mall.exchange.list")
    public Object data(@Param("searchKeyword") String searchKeyword,
                       @Param("status") ExchangeEnum status,
                       @Param("searchDate") String[] searchDate,
                       @Param("pageNumber") int pageNumber,
                       @Param("pageSize") int pageSize) {
        return Result.success().addData(exchangeService.listPage(pageNumber, pageSize, searchKeyword, searchDate, status));
    }

    /**
     * 取消订单
     * @param id String 订单id
     * @return Object
     */
    @At("/cancel/?")
    @Ok("json")
    @RequiresPermissions("user.mall.exchange.edit")
    @SLog(tag = "取消订单", msg = "订单id:${args[0]}")
    public Object editDo(@Param("id")String id) {
        exchangeService.cancel(id);
        return Result.success();
    }
    @At("/finished/?")
    @Ok("json")
    @RequiresPermissions("user.mall.exchange.edit")
    @SLog(tag = "订单完结", msg = "订单id:${args[0]}")
    public Object finished(@Param("id")String id) {
        exchangeService.update(Chain.make("status",ExchangeEnum.FINISHED)
                .add("opAt", Times.getTS()), Cnd.where("id","=",id));
        return Result.success();
    }
    @At("/doDelivery")
    @Ok("json")
    @RequiresPermissions("user.mall.exchange.edit")
    @SLog(tag = "订单发货", msg = "订单id:${args[0]}")
    public Object doDelivery(@Param("id")String id, @Param("logisticsCompany") String logisticsCompany,
                             @Param("logisticsNo") String logisticsNo, @Param("remark") String remark) {
        exchangeService.update(Chain.make("status",ExchangeEnum.DELIVERY)
                .add("logistics_company", logisticsCompany)
                .add("logistics_no", logisticsNo)
                .add("remark", remark)
                .add("opAt", Times.getTS()), Cnd.where("id","=",id));
        return Result.success();
    }

    /**
     * 查询所有类型
     * @return Object
     */
    @At("/statusList")
    @Ok("json")
    public Object statusList() {
        try {
            return Result.success().addData(ExchangeEnum.toList());
        } catch (Exception e) {
            return Result.error();
        }
    }

}